iT邦幫忙

2024 iThome 鐵人賽

DAY 5
0
AI/ ML & Data

基於人工智慧與深度學習對斑馬魚做行為分析系列 第 5

day 5 yolo多隻斑馬魚的行為分析

  • 分享至 

  • xImage
  •  

今天是第五天,我們可以寫一個對於多隻斑馬魚行為分析的yolo程式了,以下是程式碼

import cv2
import numpy as np
from scipy.spatial import distance

# 載入 YOLO 模型
def load_yolo_model():
    net = cv2.dnn.readNet("yolov3.weights", "yolov3.cfg")
    with open("coco.names", "r") as f:
        classes = [line.strip() for line in f.readlines()]
    layer_names = net.getLayerNames()
    output_layers = [layer_names[i - 1] for i in net.getUnconnectedOutLayers()]
    return net, classes, output_layers

# 偵測斑馬魚
def detect_fish(frame, net, output_layers):
    height, width, channels = frame.shape
    blob = cv2.dnn.blobFromImage(frame, 0.00392, (416, 416), (0, 0, 0), True, crop=False)
    net.setInput(blob)
    outs = net.forward(output_layers)

    class_ids = []
    confidences = []
    boxes = []

    for out in outs:
        for detection in out:
            scores = detection[5:]
            class_id = np.argmax(scores)
            confidence = scores[class_id]
            if confidence > 0.5 and class_id == 0:  # 假設斑馬魚的類別ID是0
                center_x = int(detection[0] * width)
                center_y = int(detection[1] * height)
                w = int(detection[2] * width)
                h = int(detection[3] * height)
                x = int(center_x - w / 2)
                y = int(center_y - h / 2)
                boxes.append([x, y, w, h])
                confidences.append(float(confidence))
                class_ids.append(class_id)

    indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4)
    return boxes, confidences, indexes

# 繪製偵測結果
def draw_labels(boxes, confidences, indexes, frame):
    font = cv2.FONT_HERSHEY_PLAIN
    for i in range(len(boxes)):
        if i in indexes:
            x, y, w, h = boxes[i]
            label = str(confidences[i])
            color = (0, 255, 0)
            cv2.rectangle(frame, (x, y), (x + w, y + h), color, 2)
            cv2.putText(frame, label, (x, y - 10), font, 1, color, 1)
    return frame

# 追蹤斑馬魚
def track_fish(boxes, indexes, frame, previous_positions):
    current_positions = []
    for i in range(len(boxes)):
        if i in indexes:
            x, y, w, h = boxes[i]
            center_x = x + w // 2
            center_y = y + h // 2
            current_positions.append((center_x, center_y))

    if previous_positions:
        for prev in previous_positions:
            distances = [distance.euclidean(prev, curr) for curr in current_positions]
            min_dist_index = np.argmin(distances)
            min_dist = distances[min_dist_index]
            if min_dist < 50:  # 設定一個閾值來判斷是否為同一隻斑馬魚
                cv2.line(frame, prev, current_positions[min_dist_index], (255, 0, 0), 2)
    
    return current_positions

# 主程式
def main():
    net, classes, output_layers = load_yolo_model()
    cap = cv2.VideoCapture("zebrafish_video.mp4")
    previous_positions = []

    while cap.isOpened():
        ret, frame = cap.read()
        if not ret:
            break

        boxes, confidences, indexes = detect_fish(frame, net, output_layers)
        frame = draw_labels(boxes, confidences, indexes, frame)
        current_positions = track_fish(boxes, indexes, frame, previous_positions)
        previous_positions = current_positions

        cv2.imshow("Zebrafish Detection and Tracking", frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    cap.release()
    cv2.destroyAllWindows()

if __name__ == "__main__":
    main()

1. 載入 YOLO 模型

def load_yolo_model():
    net = cv2.dnn.readNet("yolov3.weights", "yolov3.cfg")
    with open("coco.names", "r") as f:
        classes = [line.strip() for line in f.readlines()]
    layer_names = net.getLayerNames()
    output_layers = [layer_names[i - 1] for i in net.getUnconnectedOutLayers()]
    return net, classes, output_layers

這個函數的工作是把 YOLO 模型載入進來。我們需要提供 YOLO 的配置文件和權重文件,還有一個包含物體名稱的文件。這樣我們才能使用 YOLO 來偵測物體。我們還要知道哪些是 YOLO 的輸出層,這樣我們才能得到偵測結果。

2. 偵測斑馬魚

def detect_fish(frame, net, output_layers):
    height, width, channels = frame.shape
    blob = cv2.dnn.blobFromImage(frame, 0.00392, (416, 416), (0, 0, 0), True, crop=False)
    net.setInput(blob)
    outs = net.forward(output_layers)

    class_ids = []
    confidences = []
    boxes = []

    for out in outs:
        for detection in out:
            scores = detection[5:]
            class_id = np.argmax(scores)
            confidence = scores[class_id]
            if confidence > 0.5 and class_id == 0:  # 假設斑馬魚的類別ID是0
                center_x = int(detection[0] * width)
                center_y = int(detection[1] * height)
                w = int(detection[2] * width)
                h = int(detection[3] * height)
                x = int(center_x - w / 2)
                y = int(center_y - h / 2)
                boxes.append([x, y, w, h])
                confidences.append(float(confidence))
                class_ids.append(class_id)

    indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4)
    return boxes, confidences, indexes

這個函數用來偵測斑馬魚。它會先把影像轉換成 YOLO 可以處理的格式,然後把它丟進 YOLO 模型裡進行偵測。偵測出來的結果會包含每個物體的位置和信心度(即 YOLO 認為它是斑馬魚的可能性)。我們只保留信心度超過 50% 的結果。

3. 繪製偵測結果

def draw_labels(boxes, confidences, indexes, frame):
    font = cv2.FONT_HERSHEY_PLAIN
    for i in range(len(boxes)):
        if i in indexes:
            x, y, w, h = boxes[i]
            label = str(confidences[i])
            color = (0, 255, 0)
            cv2.rectangle(frame, (x, y), (x + w, y + h), color, 2)
            cv2.putText(frame, label, (x, y - 10), font, 1, color, 1)
    return frame

這個函數用來在影像上畫出偵測結果。它會在每個偵測到的斑馬魚周圍畫一個框,並在框上方顯示信心度。這樣我們就能看到 YOLO 偵測到的斑馬魚。

4. 追蹤斑馬魚

def track_fish(boxes, indexes, frame, previous_positions):
    current_positions = []
    for i in range(len(boxes)):
        if i in indexes:
            x, y, w, h = boxes[i]
            center_x = x + w // 2
            center_y = y + h // 2
            current_positions.append((center_x, center_y))

    if previous_positions:
        for prev in previous_positions:
            distances = [distance.euclidean(prev, curr) for curr in current_positions]
            min_dist_index = np.argmin(distances)
            min_dist = distances[min_dist_index]
            if min_dist < 50:  # 設定一個閾值來判斷是否為同一隻斑馬魚
                cv2.line(frame, prev, current_positions[min_dist_index], (255, 0, 0), 2)
    
    return current_positions

這個函數用來追蹤斑馬魚。它會記錄每幀影像中斑馬魚的位置,然後比較當前幀和上一幀的位置,看看哪些斑馬魚可能是同一隻。如果兩個位置之間的距離小於 50 像素,我們就認為它們是同一隻斑馬魚,並畫出它們的運動軌跡。

5. 主程式

def main():
    net, classes, output_layers = load_yolo_model()
    cap = cv2.VideoCapture("zebrafish_video.mp4")
    previous_positions = []

    while cap.isOpened():
        ret, frame = cap.read()
        if not ret:
            break

        boxes, confidences, indexes = detect_fish(frame, net, output_layers)
        frame = draw_labels(boxes, confidences, indexes, frame)
        current_positions = track_fish(boxes, indexes, frame, previous_positions)
        previous_positions = current_positions

        cv2.imshow("Zebrafish Detection and Tracking", frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    cap.release()
    cv2.destroyAllWindows()

if __name__ == "__main__":
    main()

這是主程式。它首先載入 YOLO 模型,然後打開一個包含斑馬魚的影片檔案。每讀取一幀影像,我們就用 YOLO 模型進行偵測,畫出偵測結果,再追蹤斑馬魚的運動軌跡。最後顯示處理後的影像。如果按下 'q' 鍵,程式就會結束。

總結來說,這段程式碼是用來實時偵測並追蹤影片中的斑馬魚,讓我們可以觀察它們的行為。


上一篇
day 4yolo 辨識斑馬魚行為
下一篇
day 6 yolo 模型訓練
系列文
基於人工智慧與深度學習對斑馬魚做行為分析30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言